import type { App, Plugin } from 'vue'

export const locSet = (name: string, data: any) => {
  const item = typeof data === 'string' ? data : JSON.stringify(data)
  localStorage.setItem(name, item)
}
export const locGet = (name: string, type = 'string') => {
  const data = localStorage.getItem(name)
  let result
  if (data) {
    if (type === 'string') {
      result = data
    } else if (type === 'O' || type === 'A' || type === 'N') {
      result = JSON.parse(data)
    }
  } else {
    result = data
  }
  return result
}
export const sesSet = (name: string, data: unknown) => {
  const item = typeof data === 'string' ? data : JSON.stringify(data)
  sessionStorage.setItem(name, item)
}
export const sesGet = (name: string, type = 'string') => {
  const data = sessionStorage.getItem(name)
  let result: any
  if (data) {
    if (type === 'string') {
      result = data
    } else if (type === 'O' || type === 'A' || type === 'N') {
      result = JSON.parse(data)
    }
  } else {
    result = data
  }
  return result
}

export const registerComponent = <T>(component: T) => {
  const comp = component as any
  comp.install = (app: App) => {
    app.component(comp.name || comp.displayName, component)
  }
  return component as T & Plugin
}
